﻿using DreamCMS.BLL;
using DreamCMS.Entity;
using DreamCMS.Entity.Common;
using DreamCMS.WebCore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DreamCMS.Web.Admin
{
    public partial class main : AdminPageBase
    {
        protected string OSVersion = string.Empty;
        protected string IISVersion = string.Empty;
        protected string NETVersion = string.Empty;
        protected string CPUInfo = string.Empty;
        protected string MemoryInfo = string.Empty;
        protected string ServerIP = ServerHelper.ServerIP;
        protected string UserIP = RequestHelper.UserIP;
        protected string UpfilePath = ConfigHelper.SiteRootPath + "uploads/";
        protected long UpfileCount = 0;
        protected string UpfileSize = string.Empty;
        protected string DbInfo = string.Empty;
        protected string DbPhyPath = ConfigHelper.AccessDbPhysicPath; //数据库物理路径
        
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckStatistic();
            this.GetOSInfo();
            this.UpfileCount = IOHelper.GetDirectoryCount(Server.MapPath(UpfilePath));
            this.UpfileSize = IOHelper.GetFileSize(IOHelper.GetDirectorySize(Server.MapPath(UpfilePath)));
            this.DbInfo = ConfigHelper.AccessDbRelativePath +" "+ IOHelper.GetFileSize(IOHelper.GetFileLength(DbPhyPath));
        }

        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref   MEMORY_INFO meminfo);
        //定义内存的信息结构   
        [StructLayout(LayoutKind.Sequential)]
        public struct MEMORY_INFO
        {
            public uint dwLength;
            public uint dwMemoryLoad;
            public uint dwTotalPhys;
            public uint dwAvailPhys;
            public uint dwTotalPageFile;
            public uint dwAvailPageFile;
            public uint dwTotalVirtual;
            public uint dwAvailVirtual;
        }
        private void GetOSInfo()
        {
            try
            {
                OSVersion = Environment.OSVersion.ToString();

                IISVersion = Request.ServerVariables["SERVER_SOFTWARE"];

                if (OSVersion.IndexOf("Microsoft Windows NT 5.0") > -1)
                {
                    OSVersion = string.Concat("Microsoft Windows 2000 (", OSVersion, ")");
                    IISVersion = "IIS 5";
                }
                else if (OSVersion.IndexOf("Microsoft Windows NT 5.1") > -1)
                {
                    OSVersion = string.Concat("Microsoft Windows XP (", OSVersion, ")");
                    IISVersion = "IIS 5.1";
                }
                else if (OSVersion.IndexOf("Microsoft Windows NT 5.2") > -1)
                {
                    OSVersion = string.Concat("Microsoft Windows 2003 (", OSVersion, ")");
                    IISVersion = "IIS 6";
                }
                else if (OSVersion.IndexOf("Microsoft Windows NT 6.0") > -1)
                {
                    OSVersion = string.Concat("Microsoft Windows Vista or Server 2008 (", OSVersion, ")");
                    IISVersion = "IIS 7";
                }
                else if (OSVersion.IndexOf("Microsoft Windows NT 6.1") > -1)
                {
                    OSVersion = string.Concat("Microsoft Windows 7 or Server 2008 R2 (", OSVersion, ")");
                    IISVersion = "IIS 7.5";
                }
                else if (OSVersion.IndexOf("Microsoft Windows NT 6.2") > -1)
                {
                    OSVersion = string.Concat("Microsoft Windows 8/8.1 or Server 2012  (", OSVersion, ")");
                    IISVersion = "IIS 8.0";
                }

                NETVersion = Environment.Version.ToString();

                CPUInfo = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER") + " (" + Environment.ProcessorCount + " 核)";

                MEMORY_INFO MemInfo;
                MemInfo = new MEMORY_INFO();
                GlobalMemoryStatus(ref   MemInfo);


                MemoryInfo = "物理内存:" + (MemInfo.dwTotalPhys / 1024 / 1024) + " MB / 当前程序已占用物理内存:" + (Environment.WorkingSet / 1024 / 1024).ToString() + " MB";
            }
            catch
            {

            }
        }


        /// <summary>
        /// 清理缓存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void clearCache_Click(object sender, EventArgs e)
        {
            UserManager.ClearCache();
            PostManager.ClearCache();
            StatisticManager.ClearCache();
            CategoryManager.ClearCache();
            TagManager.ClearCache();
            Response.Write("<script>top.location.href='default.aspx';</script>");
        }

        /// <summary>
        /// 检查系统统计表与实际的实体数据统计信息是否相符，不符则改正
        /// </summary>
        protected void CheckStatistic()
        {
            StatisticInfo s = StatisticManager.GetStatisticInfo();
            bool update = false;

            long totalPosts = PostManager.GetPostCount(-1, -1, -1,PostStatus.Published, PostHideStatus.NoHide);
            if (totalPosts != s.PostCounts)
            {
                s.PostCounts = totalPosts;
                update = true;
            }

            long totalComments = CommentManager.GetCommentCount(true);
            if (totalComments != s.CommentCounts)
            {
                s.CommentCounts = totalComments;
                update = true;
            }
            int totalTags = TagManager.GetTagList().Count;
            if (totalTags != s.TagCounts)
            {
                s.TagCounts = totalTags;
                update = true;
            }
            if (update == true)
            {
                StatisticManager.UpdateStatisticInfo();
            }
        }
    }
}